package com.qy.StudentInfo.handler;

import com.qy.StudentInfo.mapper.AdminMapper;
import com.qy.StudentInfo.mapper.WangMapper;
import com.qy.StudentInfo.pojo.Student;
import com.qy.StudentInfo.pojo.Teacher;
import com.qy.StudentInfo.pojo.Wang;
import com.qy.StudentInfo.service.*;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ProjectName: StudentInfo
 * @Package: com.qy.StudentInfo.handler
 * @ClassName: AdminHandler
 * @Description: admin的handler层（servlet）
 * @Author: 齐玥
 * @CreateDate: 2021-10-10 14:24
 * @UpdateUser: 齐玥
 * @UpdateDate: 2021-10-15 22:50
 * @UpdateRemark: 新建
 * @Version: 1.0
 */

@Controller
@RequestMapping("/AdminHandler")
//@SessionAttributes("studentList")
//@SessionAttributes("teacherList")
public class AdminHandler {

	private static Logger log = Logger.getLogger(AdminHandler.class.getClass());

    @Autowired
	StudentService studentService;
	@Autowired
	TeacherService teacherService;
	@Autowired
	CoursePlanService coursePlanService;
	@Autowired
    CourseService courseService;
	@Autowired
	SelectCourseService selectCourseService;
	@Autowired
	AdminMapper adminMapper;
	@Autowired
	private WangService wangService;
	@Autowired
	private WangMapper wangMapper;

	/**
	 * JavaScript学的不好，目前只能通过这种方式把三级联动下拉列表的val改变为以下值传给数据库。
	 * 
	 */
	String[] arr_belongcoll = {"数学与计算机科学学院", "物理与智能制造学院", "经济与管理学院", "法学与商务学院"};
	String[][] arr_belongpro = {
                    {"移动云计算", "信息与计算科学（智能移动开发）", "服务外包", "数据科学与大数据技术"},
                    {"应用物理学(集成电路方向)", "物联网工程", "电子信息工程", "物理学"},
                    {"会计学", "会计学（中外合作办学）", "会计学（金融服务与理财方向）", "会计学（国际班）"},
                    {"法学专业", "思想政治教育专业", "旅游管理专业", "电子商务专业"}
				};
	String[][][] arr_belongcla = {
                {
                    {"移动云计算一班", "移动云计算二班"},
                    {"智能移动开发一班", "智能移动开发二班"},
                    {"服务外包一班", "服务外包二班"},
                    {"大数据一班", "大数据二班"}
                    
                },
                {
                    {"应用物理学一班", "应用物理学二班"},
                    {"物联网工程一班", "物联网工程二班"},
                    {"电子信息工程一班", "电子信息工程二班"},
                    {"物理学一班", "物理学二班"}
                    
                },
                {
                    {"会计学一班", "会计学二班"},
                    {"会计学（中外合作办学）一班", "会计学（中外合作办学）二班"},
                    {"会计学（金融服务与理财方向）一班", "会计学（金融服务与理财方向）二班"},
                    {"会计学（国际班）一班", "会计学（国际班）二班"}
                    
				},
                {
                    {"法学一班", "法学二班"},
                    {"思想政治教育专业一班", "思想政治教育专业二班"},
                    {"旅游管理专业一班", "旅游管理专业二班"},
                    {"电子商务专业一班", "电子商务专业二班"}
                    
				}
		};

 // 添加
 	@RequestMapping("/addStudent")
 	public String addStudent(Student student, Model model) {
 		int col = Integer.parseInt(student.getCollege());
 		int pro = Integer.parseInt(student.getProfession());
 		int cla = Integer.parseInt(student.getClassr());
	 		student.setCollege(arr_belongcoll[pro]);
	 		student.setProfession(arr_belongpro[pro][col]);
	 		student.setClassr(arr_belongcla[pro][col][cla]);
 		
 		
 		
 		if (studentService.insertStudent(student) != 0) {
 			model.addAttribute("student", student);
 			log.info("------------添加成功!------------");
 			return "modisuccess";
 			// return "admin/addStudent";
 		} else {
 			return "fail";
 		}

 	}
 	//查询全部学生方法
 	public void queryStu(HttpServletRequest request) {
 		List<Student> studentList = new ArrayList<Student>();
 		studentList = studentService.selectStudentBySql(1,10);
		request.setAttribute("slist", studentList);
 	}

 	public void pageIn(Model model,List list) {
 		PageInfo page = new PageInfo(list, 5);
	 	model.addAttribute("pageInfo", page);
 	}
 	
 	// 查询
 	@RequestMapping(value = "/query/{pn}", method = RequestMethod.GET)
 	public String redirect(@RequestParam("serc") String serc, @RequestParam("condition") String condition,HttpServletRequest request,
 			@PathVariable(value = "pn") String pn,Model model) {

 		int no = Integer.parseInt(pn);
// 		System.out.println("-----"+no+"----");
 		List<Student> studentList = new ArrayList<Student>();
 		PageHelper.startPage(no, 5);
 		request.setAttribute("serc", serc);
		request.setAttribute("condition", condition);
 		//查询全部
 		if (serc.equals("all")) {
			log.info("[查询全部学生方法] 查询开始!!!!!!");
			studentList = studentService.selectStudentBySql(1,10);
			log.info("[查询全部学生方法] 查询结束!!!!!!"+studentList.size());
			pageIn(model, studentList);
 	 		request.setAttribute("slist", studentList);
 			return "admin/queryStudent";

 		//根据学号查询
 		} else if (serc.equals("sid")) {

 			studentList = studentService.getByStudentSid(1,10,condition);
 			pageIn(model, studentList);
 			request.setAttribute("slist", studentList);
 			System.out.println("sid");
 			return "admin/queryStudent";

 		//根据学院查询
 		} else if (serc.equals("col")) {
 			studentList = studentService.getByStudentCol(1,10,condition);
 			pageIn(model, studentList);
 			request.setAttribute("slist", studentList);
 			System.out.println(studentList);
 			System.out.println("col");
 			return "admin/queryStudent";

 		//根据专业查询
 		} else if (serc.equals("pro")) {
 			studentList = studentService.getByStudentPro(1,10,condition);
 			pageIn(model, studentList);
 			request.setAttribute("slist", studentList);
 			System.out.println(studentList);
 			System.out.println("pro");
 			return "admin/queryStudent";

 		//根据班级查询
 		} else if (serc.equals("cla")) {
 			studentList = studentService.getByStudentCla(1,10,condition);
 			pageIn(model, studentList);
 			request.setAttribute("slist", studentList);
 			return "admin/queryStudent";

 		} else {
 			studentList = studentService.selectStudentBySql(1,10);
 			pageIn(model, studentList);
 	 		request.setAttribute("slist", studentList);
 			return "admin/queryStudent";

 		}

 	}

 	// 删除学生
 	@RequestMapping(value = "/delete/{sid}", method = RequestMethod.GET)
 	public String deleteStudent(@PathVariable(value = "sid") String sid, Model model) {


 		if (studentService.deleteStudent(sid) != 0) {
 			System.out.println("success");
 			
 			return "delsuccess";
 		} else {
 			System.out.println("fail");
 			return "fail";
 		}

 	}

 	// 跳转页面
 	@RequestMapping(value = "/finalPage", method = RequestMethod.GET)
 	public String finalPage(HttpServletRequest request) {
 		queryStu(request);
 		return "admin/queryStudent";
 	}

 	// 修改定位
 	@RequestMapping(value = "/moditystu/{sid}", method = RequestMethod.GET)
 	public String editPre(@PathVariable("sid") String sid, HttpServletRequest request) {

 		List<Student> studentList = new ArrayList<Student>();
 		studentList = studentService.getByStudentSid(1,10,sid);
 		
 		request.setAttribute("studentList", studentList);
 		System.out.println("-----进入修改");
 		return "admin/modiStudent";
 	}

 	
 	
 	// 修改
 	@RequestMapping(value = "/moditystud/{sid}", method = RequestMethod.GET)
 	public String update(@PathVariable("sid") String sid, Student student, HttpServletRequest request) {

 		int col = Integer.parseInt(student.getCollege());
 		int pro = Integer.parseInt(student.getProfession());
 		int cla = Integer.parseInt(student.getClassr());
 		student.setCollege(arr_belongcoll[pro]);
 		student.setProfession(arr_belongpro[pro][col]);
 		student.setClassr(arr_belongcla[pro][col][cla]);
 		
 		if (studentService.modifyStudent(student) != 0) {
 			System.out.println("----修改成功--------------------------------------------------------------------------------------------------------");
 			return "modisuccess";
 		} else {
 			System.out.println("----修改失败----------------------------------------------------------------");
 			return "fail";
 		}
 	}
 	
 	
 	

 	// 跳转页面
 	@RequestMapping("/managestu/{pn}")
 	public String manageStudent(HttpServletRequest request,
 			@PathVariable(value = "pn") String pn,Model model) {
 		int no = Integer.parseInt(pn);
 		
 		PageHelper.startPage(no, 5);
 		List<Student> studentList = new ArrayList<Student>();
 		studentList = studentService.selectStudentBySql(1,100);
 		pageIn(model, studentList);
 		request.setAttribute("slist", studentList);
 		return "admin/queryStudent";
 	}
 		
 	// 跳转页面
 	@RequestMapping("/managetea/{pn}")
 	public String manageTeacher(HttpServletRequest request,
 			@PathVariable(value = "pn") String pn,Model model) {
 		int no = Integer.parseInt(pn);
 		PageHelper.startPage(no, 5);
 		List<Teacher> teacherList = new ArrayList<Teacher>();
	 	teacherList = teacherService.selectTeacherBySql(1,10);
	 	pageIn(model, teacherList);
	 	request.setAttribute("teacherList", teacherList);
 		return "admin/queryTeacher";
 	}


 	// 跳转页面
 	@RequestMapping("/addstu")
 	public String adStudent() {
 		return "admin/addStudent";
 	}

 	// 跳转页面
 	@RequestMapping("/addtea")
 	public String adTeacher() {
 		return "admin/addTeacher";
 	}

 	// 跳转页面
 	@RequestMapping("/addcou")
 	public String adCourse() {
 		return "admin/addCourse";
 	}

 	// 跳转页面
 	@RequestMapping("/addanno")
 	public String adAddanno() {
 		return "admin/adAnnouncement";
 	}

	/**
	 * 添加教师
	 * @param teacher
	 * @param model
	 * @param httpSession
	 * @return
	 */
 	@RequestMapping("/addTeacher")
 	public String addTeacher(Teacher teacher, Model model, HttpSession httpSession) {

 		if (teacherService.insertTeacher(teacher) != 0) {
 			model.addAttribute("teacher", teacher);

 			return "modisuccess";
 			//return new ModelAndView(new RedirectView("/StudentInfo/TeacherHandler/finalPage"));
 			// return "techer/teacherFace";
 		} else {
 			return "fail";
 			//return new ModelAndView(new RedirectView("fail"));
 		}

 	}

 	
 	/**
 	 * 教师相关
 	 */
 	
 	//查询全部教师方法
 	 	public void queryTea(HttpServletRequest request) {
 	 		List<Teacher> teacherList = new ArrayList<Teacher>();
 	 		teacherList = teacherService.selectTeacherBySql(1,10);
 	 		request.setAttribute("teacherList", teacherList);
 	 	}

	/**
	 * 查询
	 * @param serc
	 * @param condition
	 * @param request
	 * @param pn
	 * @param model
	 * @return
	 */
 	@RequestMapping(value = "/queryTea/{pn}", method = RequestMethod.GET)
 	public String redirectTea(@RequestParam("serc") String serc, @RequestParam("condition") String condition,HttpServletRequest request,
 			@PathVariable(value = "pn") String pn,Model model) {
 		int no = Integer.parseInt(pn);
 		PageHelper.startPage(no, 5);
 		List<Teacher> teacherList = new ArrayList<Teacher>();
 		request.setAttribute("serc", serc);
		request.setAttribute("condition", condition);
 		
 		if (serc.equals("all")) {

 			teacherList = teacherService.selectTeacherBySql(1,10);
 			pageIn(model, teacherList);
 	 		request.setAttribute("teacherList", teacherList);
 			return "admin/queryTeacher";

 		} else if (serc.equals("tid")) {

 			teacherList = teacherService.getByTeacherTid(1,10,condition);
 			pageIn(model, teacherList);
 			request.setAttribute("teacherList", teacherList);
 			System.out.println("tid");

 			return "admin/queryTeacher";

 		} else {

 			teacherList = teacherService.selectTeacherBySql(1,10);
 			pageIn(model, teacherList);
 	 		request.setAttribute("teacherList", teacherList);
 			return "admin/queryTeacher";

 		}

 	}


	/**
	 * 删除教师
	 * @param tid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/deleteTea/{tid}", method = RequestMethod.GET)
 	public String deleteTeacher(@PathVariable(value = "tid") String tid, Model model) {


 		if (teacherService.deleteTeacher(tid) != 0) {
 			System.out.println("success");
 			return "delsuccess";
 		} else {
 			System.out.println("fail");
 			return "fail";
 			
 		}

 	}

	/**
	 * 根据查询条件教师
	 * @param request
	 * @return
	 */
 	@RequestMapping(value = "/finalPageTea", method = RequestMethod.GET)
 	public String finalPageTea(HttpServletRequest request) {
 		queryTea(request);
 		return "admin/queryTeacher";
 	}

	/**
	 * 修改定位
	 * @param tid
	 * @param request
	 * @return
	 */
 	@RequestMapping(value = "/modityTea/{tid}", method = RequestMethod.GET)
 	public String editPreTea(@PathVariable("tid") String tid, HttpServletRequest request) {

 		List<Teacher> teacherList = new ArrayList<Teacher>();
 		teacherList = teacherService.getByTeacherTid(1,10,tid);
 		//model.addAttribute("teacherList", teacherList);
 		request.setAttribute("teacherList", teacherList);

 		return "admin/modiTeacher";
 	}

	/**
	 * 修改教师
	 * @param tid
	 * @param teacher
	 * @param request
	 * @return
	 */
 	@RequestMapping(value = "/modityTeac/{tid}", method = RequestMethod.GET)
 	public String update(@PathVariable("tid") String tid, Teacher teacher, HttpServletRequest request) {

 		if (teacherService.modifyTeacher(teacher) != 0) {
 			return "modisuccess";
 		} else {
 			return "fail";
 		}
 	}

	/**
	 * 添加公告
	 * @param announ
	 * @param request
	 * @return
	 */
 	@RequestMapping(value = "/addAnnouncement")
	public String addAnn(@Param("announ") String announ,HttpServletRequest request){
 		request.setAttribute("announ",announ);
		if (wangMapper.interAnnoun(announ) != 0){
			return "modisuccess";
		}else {
			return "fail";
		}

	}

	/**
	 * 查看所有公告
	 * @param id
	 * @param request
	 * @param pn
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/selAnnouncement/{pn}")
	public String selAnn(@Param("id") String id,HttpServletRequest request,@PathVariable(value = "pn") String pn,Model model){
		int no = Integer.parseInt(pn);
		PageHelper.startPage(no, 5);
 		//查询所有的公告
		System.out.println("开始进行查询公告!!!!!!!!");
		List<Wang> wangList = new ArrayList<>();
		wangList =wangService.getByTeacherTid(1,5,id);
		pageIn(model, wangList);
//		log.info("查训的结果为"+wangList);
		System.out.println("查训的结果为"+wangList);
		request.setAttribute("wangList",wangList);
		return "admin/adAnnouncement";
	}
}
